﻿using Microsoft.AspNetCore.Mvc;
using ThridGroup.ERP.ProductionSystem;
using ThridGroup.ERP.DTO.SupplierDto;
using System.Threading.Tasks;
using System.Collections.Generic;

namespace ThridGroup.ERP.Web.Controllers.ProductionSystem
{
    /// <summary>
    /// 供应商管理   ---tsh
    /// </summary>
    [Route("ProductionSystem/[controller]/[action]")]
    public class SupplierModelController : Controller
    {
        private readonly ISupplierModelServices _supplierModelServices;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="supplierModelServices">供应商服务</param>
        public SupplierModelController(ISupplierModelServices supplierModelServices)
        {
            _supplierModelServices = supplierModelServices;
        }

        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 供应商管理页面
        /// </summary>
        /// <returns>返回视图内容</returns>
        public IActionResult SupplierModelView()
        {
            return View("~/Views/ProductionSystem/SupplierModelView.cshtml");
        }

        /// <summary>
        /// 获取供应商分页列表
        /// </summary>
        /// <param name="page">页码</param>
        /// <param name="limit">每页数量</param>
        /// <param name="supplierCode">供应商编码</param>
        /// <param name="supplierName">供应商名称</param>
        /// <param name="supplierGrade">供应商等级</param>
        /// <param name="supplierPhone">供应商电话</param>
        /// <param name="isEnable">是否启用</param>
        /// <returns>分页数据</returns>
        [HttpGet]
        public async Task<IActionResult> GetSupplierList(
            int page = 1,
            int limit = 10,
            string supplierCode = "",
            string supplierName = "",
            string supplierGrade = "",
            string supplierPhone = "",
            bool? isEnable = null)
        {
            var query = new QuerySupplierDto
            {
                Page = page,
                Limit = limit,
                SupplierCode = supplierCode,
                SupplierName = supplierName,
                SupplierGrade = supplierGrade,
                SupplierPhone = supplierPhone,
                IsEnable = isEnable
            };

            var result = await _supplierModelServices.GetSupplierPagedListAsync(query);
            return Json(result);
        }

        /// <summary>
        /// 根据ID获取供应商信息
        /// </summary>
        /// <param name="id">供应商ID</param>
        /// <returns>供应商信息</returns>
        [HttpGet]
        public async Task<IActionResult> GetSupplierById(int id)
        {
            try
            {
                var supplier = await _supplierModelServices.GetSupplierByIdAsync(id);
                if (supplier == null)
                {
                    return Json(new { success = false, message = "供应商不存在" });
                }

                return Json(new { success = true, data = supplier });
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 创建供应商
        /// </summary>
        /// <param name="input">创建供应商输入</param>
        /// <returns>创建结果</returns>
        [HttpPost]
        public async Task<IActionResult> CreateSupplier([FromBody] CreateSupplierDto input)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { success = false, message = "数据验证失败" });
                }

                var success = await _supplierModelServices.CreateSupplierAsync(input);
                if (success)
                {
                    return Json(new { success = true, message = "创建成功" });
                }
                else
                {
                    return Json(new { success = false, message = "创建失败" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 更新供应商
        /// </summary>
        /// <param name="input">更新供应商输入</param>
        /// <returns>更新结果</returns>
        [HttpPut]
        public async Task<IActionResult> UpdateSupplier([FromBody] UpdateSupplierDto input)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return Json(new { success = false, message = "数据验证失败" });
                }

                var success = await _supplierModelServices.UpdateSupplierAsync(input);
                if (success)
                {
                    return Json(new { success = true, message = "更新成功" });
                }
                else
                {
                    return Json(new { success = false, message = "更新失败" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 删除供应商
        /// </summary>
        /// <param name="id">供应商ID</param>
        /// <returns>删除结果</returns>
        [HttpDelete]
        public async Task<IActionResult> DeleteSupplier(int id)
        {
            try
            {
                var success = await _supplierModelServices.DeleteSupplierAsync(id);
                if (success)
                {
                    return Json(new { success = true, message = "删除成功" });
                }
                else
                {
                    return Json(new { success = false, message = "删除失败" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 自动生成供应商编码
        /// </summary>
        /// <returns>供应商编码</returns>
        [HttpGet]
        public async Task<IActionResult> GenerateSupplierCode()
        {
            try
            {
                var code = await _supplierModelServices.GenerateSupplierCodeAsync();
                return Json(new { success = true, code = code });
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }

        /// <summary>
        /// 批量删除供应商
        /// </summary>
        /// <param name="ids">供应商ID列表</param>
        /// <returns>删除结果</returns>
        [HttpPost]
        public async Task<IActionResult> BatchDeleteSupplier([FromBody] List<int> ids)
        {
            try
            {
                if (ids == null || ids.Count == 0)
                {
                    return Json(new { success = false, message = "请选择要删除的数据" });
                }

                var success = await _supplierModelServices.BatchDeleteSupplierAsync(ids);
                if (success)
                {
                    return Json(new { success = true, message = "批量删除成功" });
                }
                else
                {
                    return Json(new { success = false, message = "批量删除失败" });
                }
            }
            catch (System.Exception ex)
            {
                return Json(new { success = false, message = ex.Message });
            }
        }
    }
}
